<template>
  <!-- 试验委托 -->
  <div class="app-container">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="试验类型" prop="experimentType">
          <el-select
            v-model="filters.experimentType"
            size="small"
            filterable
            placeholder="请选择试验类型"
            clearable
          >
            <el-option
              v-for="dict in TypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button id="Query" v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button id="ResetQuery" v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-plus" size="middle" @click="handleAdd">新增</el-button>
      <el-button icon="el-icon-edit" size="middle" :disabled="single" @click="handleUpdate">修改</el-button>
      <el-button id="Delete" icon="el-icon-delete" size="middle" :disabled="single" @click="handleDelete">删除</el-button>
      <el-button id="Detail" icon="el-icon-zoom-in" size="middle" :disabled="single" @click="handleDetail">详情</el-button>
      <el-button icon="el-icon-document" size="middle" :disabled="single" @click="TestResults">成果报告</el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column
        prop="experimentType"
        :show-overflow-tooltip="true"
        :formatter="formatExperimentType"
        label="试验类型"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="companyName"
        :show-overflow-tooltip="true"
        label="试验单位"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="clientEnterpriseId"
        :show-overflow-tooltip="true"
        label="委托单位"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="finishDate"
        :show-overflow-tooltip="true"
        label="完成日期"
        min-width="150"
        align="center"
      />
    </el-table>
    <!-- 表格展示end-->

    <!-- 分页器 -->
    <pagination
      class="pagination-container"
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <TestResults ref="TestResults" />
  </div>
</template>

<script>
import TestResults from './components/TestResults'
import { listTest, deleteTest } from '@/api/projectManage/ProjectAllocation/testCommission'
import { mapState } from 'vuex'
export default {
  name: 'AssignCompany',
  components: {
    TestResults
  },
  data() {
    return {
      // 筛选条件
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        pageNum: 1,
        pageSize: 20,
        experimentType: '' // 试验名称
      },
      TypeOptions: [
        {
          dictValue: '0',
          dictLabel: '标准固结实验'
        },
        {
          dictValue: '1',
          dictLabel: '快速固结实验'
        },
        {
          dictValue: '2',
          dictLabel: '其他'
        }
      ],
      TestOptions: [],
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [] // 表格数据
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    })
  },
  created() {
    this.getList()
  },
  methods: {
    formatExperimentType(row) {
      return this.selectDictLabel(this.TypeOptions, row.experimentType)
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    // 获取数据
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listTest(this.filters)
        this.tableData = rows
        this.total = total
        // this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 按钮 Start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 新增
    handleAdd() {
      const { proName, proId } = this.$route.query
      this.$router.push({
        path: '/newProject/assignCompany-add',
        query: { proId, proName }
      })
    },
    // 详情
    handleDetail() {
      const { proName, proId } = this.$route.query
      const id = this.multipleSelection[0].id
      this.$router.push({
        path: '/newProject/assignCompany-detail',
        query: { id, proName, proId }
      })
    },
    // 编辑
    handleUpdate() {
      const { proName, proId } = this.$route.query
      const id = this.multipleSelection[0].id
      this.$router.push({
        path: '/newProject/assignCompany-add',
        query: { id, proId, proName }
      })
    },
    // 删除
    handleDelete() {
      this.$confirm('确认要删除此试验吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.loading = true
          // 通过id删除
          await deleteTest(this.multipleSelection[0].id)
          this.loading = false
          this.msgSuccess('删除成功！')
          this.getList()
        })
        .catch(() => {
          this.msgWarning('已取消删除')
        })
    },
    // 成果报告
    TestResults() {
      this.$refs.TestResults.handleAdd('成果报告', this.multipleSelection[0].id)
    }
  }
}
</script>

<style scoped lang="scss">
</style>

